package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.ZQPreviewDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ZQPreviewModelAdapter extends RecyclerView.Adapter<ZQPreviewModelAdapter.PreviewModelVH> {

    public static final int TEXT = 1;
    public static final int PIC = 2;
    public static final int CATEGORY = 0;
    private static final int HEADER = 4;
    private static final int SUBMIT_BTN = 5;

    private List<ZQPreviewDetail> mPreviewDatas;
    private boolean hasHeader;
    private LayoutInflater mLayoutInflater;
    private ZQPreviewItemAdapter mZqPreviewHeaderAdapter;
    private ZQPreviewItemAdapter mZqPreviewSubmitBtnAdapter;

    private ZQPreviewModelAdapter(Context context, List<ZQPreviewDetail> previewDatas, ZQPreviewItemAdapter zqPreviewHeaderAdapter, ZQPreviewItemAdapter zqPreviewSubmitBtnAdapter) {
        mPreviewDatas = previewDatas;
        hasHeader = zqPreviewHeaderAdapter != null;
        mZqPreviewHeaderAdapter = zqPreviewHeaderAdapter;
        mZqPreviewSubmitBtnAdapter = zqPreviewSubmitBtnAdapter;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public static class Builder {

        private Context context;
        private List<ZQPreviewDetail> previewDatas;
        private ZQPreviewItemAdapter zqPreviewHeaderAdapter;
        private ZQPreviewItemAdapter zqPreviewSubmitBtnAdapter;

        public Builder(@NonNull Context context) {
            this.context = context;
        }

        public Builder setPreviewDatas(@NonNull List<ZQPreviewDetail> previewDatas) {
            this.previewDatas = previewDatas;
            return this;
        }

        public Builder setZqPreviewHeader(@NonNull ZQPreviewItemAdapter zqPreviewHeaderAdapter) {
            this.zqPreviewHeaderAdapter = zqPreviewHeaderAdapter;
            return this;
        }

        public Builder setZqPreviewSubmitBtn(@NonNull ZQPreviewItemAdapter zqPreviewSubmitBtnAdapter) {
            this.zqPreviewSubmitBtnAdapter = zqPreviewSubmitBtnAdapter;
            return this;
        }

        public ZQPreviewModelAdapter build() {
            if (previewDatas == null) previewDatas = new ArrayList<>();
            if (zqPreviewSubmitBtnAdapter == null) zqPreviewSubmitBtnAdapter = new ZQPreviewItemAdapter() {
                @Override
                public View createView(LayoutInflater mLayoutInflater, ViewGroup viewGroup) {
                    return null;
                }

                @Override
                public void bindVh(PreviewModelVH previewModelVH) {

                }
            };
            return new ZQPreviewModelAdapter(context, previewDatas, zqPreviewHeaderAdapter, zqPreviewSubmitBtnAdapter);
        }
    }

    @NonNull
    @Override
    public PreviewModelVH onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        switch (viewType) {
            case HEADER:
                if (mZqPreviewHeaderAdapter != null)
                    return new PreviewModelVH(mZqPreviewHeaderAdapter.createView(mLayoutInflater, viewGroup));
                else throw new RuntimeException("列表头部缺少对应的ZqPreviewHeaderAdapter");
            case SUBMIT_BTN:
                if (mZqPreviewSubmitBtnAdapter != null)
                    return new PreviewModelVH(mZqPreviewSubmitBtnAdapter.createView(mLayoutInflater, viewGroup));
                else throw new RuntimeException("列表底部缺少对应的mZqPreviewSubmitBtnAdapter");
            case CATEGORY:
                return new PreviewModelVH(mLayoutInflater.inflate(R.layout.item_zq_preview_category, viewGroup, false));
            case PIC:
                return new PreviewModelVH(mLayoutInflater.inflate(R.layout.item_zq_preview_pic, viewGroup, false));
            case TEXT:
                return new PreviewModelVH(mLayoutInflater.inflate(R.layout.item_zq_preview_text, viewGroup, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull PreviewModelVH previewModelVH, int position) {
        if (hasHeader) {
            if (position == 0) {
                if (mZqPreviewHeaderAdapter != null) mZqPreviewHeaderAdapter.bindVh(previewModelVH);
            }
            else position--;
        }
        if (position == mPreviewDatas.size()) {
            if (mZqPreviewSubmitBtnAdapter != null) mZqPreviewSubmitBtnAdapter.bindVh(previewModelVH);
        }
        else {
            ZQPreviewDetail detail = mPreviewDatas.get(position);
            previewModelVH.bindDetail(detail);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (hasHeader) {
            if (position == 0) return HEADER;
            else position--;
        }
        if (position == mPreviewDatas.size()) return SUBMIT_BTN;
        else return mPreviewDatas.get(position).getPreviewType();
    }

    @Override
    public int getItemCount() {
        int size = mPreviewDatas.size();
        if (hasHeader) size++;
        size++;
        return size;
    }

    static class PreviewModelVH extends RecyclerView.ViewHolder {

        @BindView(R.id.label)
        TextView mLabel;
        @BindView(R.id.text)
        TextView mTv;
        @BindView(R.id.img)
        ImageView mImg;
        @BindView(R.id.required_view)
        View mRequiredView;

        PreviewModelVH(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void bindDetail(ZQPreviewDetail detail) {
            mLabel.setText(detail.getLabel());
            switch (detail.getPreviewType()) {
                case TEXT:
                    mTv.setText(detail.getContent());
                    mRequiredView.setVisibility(detail.isRequired() ? View.VISIBLE : View.GONE);
                    break;
                case PIC:
                    Glide.with(itemView.getContext()).load(detail.getContent())
                            .placeholder(R.drawable.nopic)
                            .error(R.drawable.nopic).into(mImg);
                    mImg.setOnClickListener(v -> {
                        //todo 看大图
                    });
                    mRequiredView.setVisibility(detail.isRequired() ? View.VISIBLE : View.GONE);
                    break;
            }
        }

    }
}
